<template>
  <div class="login-page">
    <!-- 固定导航栏 -->
    <van-nav-bar title="登录" left-arrow fixed placeholder @click-left="router.back()" class="custom-nav" />
    <div class="login-content">
      <div class="login-header">
        <img src="@/assets/images/logo.png" alt="logo" class="logo">
        <h2 class="welcome">欢迎回来</h2>
        <p class="subtitle">请使用您的账号登录</p>
      </div>

      <van-form @submit="onSubmit" ref="formRef">
        <div class="login-form">
          <div class="input-wrapper">
            <van-field
              v-model="formData.username"
              placeholder="请输入账号"
              class="custom-field"
              :rules="[{ required: true, message: '请输入账号',trigger: ['onBlur', 'onChange']}]"
              :border="false"
            >
              <template #left-icon>
                <van-icon name="user-o" />
              </template>
            </van-field>
            <van-field
              v-model="formData.password"
              :type="showPassword ? 'text' : 'password'"
              placeholder="请输入密码"
              class="custom-field"
              :rules="passwordRules"
              :border="false"
            >
              <template #left-icon>
                <van-icon name="lock" />
              </template>
              <template #right-icon>
                <van-icon 
                  :name="showPassword ? 'eye-o' : 'closed-eye'" 
                  @click="showPassword = !showPassword"
                  class="eye-icon"
                />
              </template>
            </van-field>
          </div>

          <van-button 
            block 
            round 
            type="primary" 
            native-type="submit"
            class="login-btn"
            :loading="loading"
          >
            登录
          </van-button>

          <div class="action-links">
            <span class="link" @click="router.push('/auth/forgot-password')">忘记密码</span>
            <span class="link" @click="router.push('/auth/register')">注册账号</span>
          </div>
        </div>
      </van-form>
      <div class="other-login">
        <div class="divider">其他登录方式</div>
        <div class="login-options">
          <button class="option-btn" @click="router.push('/auth/phone-login')">
            手机号登录
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { showToast } from 'vant'
import { useUserStore } from '@/stores/user'
import { passwordRules } from '@/utils/validate'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const formRef = ref()
const showPassword = ref(false)

const formData = reactive({
  username: '',
  password: ''
})
const loading = ref(false)

const onSubmit = async () => {
  try {
    loading.value = true
    console.log('开始登录...');
    await userStore.login(formData)
    console.log('登录成功，获取用户信息...');
    showToast('登录成功')
    
    // 如果有重定向参数，跳转到重定向页面
    const redirect = route.query.redirect as string
    router.push(redirect || '/home')
  } catch (error: any) {
    console.error('登录失败:', error);
    // 直接使用错误信息
    showToast(error.message)
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
.login-page {
  min-height: 100vh;
}

.login-content {
  padding: 30px 20px 30px; // 顶部padding增加，为导航栏留出空间
}

.login-header {
  text-align: center;
  margin-bottom: 25px;

  .logo {
    width: 72px;
    height: 72px;
    border-radius: 16px;
    margin-bottom: 16px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }

  .welcome {
    font-size: 22px;
    font-weight: 600;
    color: #333;
  }

  .subtitle {
    font-size: 14px;
    color: #666;
  }
}

.login-form {
  margin-bottom: 45px;
  .input-wrapper {
    background: rgba(255, 255, 255, 0.9);
    border-radius: 16px;
    padding: 12px 16px;
    margin-bottom: 25px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
    position: relative;

    :deep(.custom-field) {
      background: transparent;
      padding: 5px 0;
      // height: 50px;
      // margin-bottom: 12px;
      border-bottom: 1px solid rgba(0, 0, 0, 0.1);
      
      // 优化错误提示样式
      .van-field__error-message {
        position: absolute;
        left: 0px; // 与输入框文本对齐
        bottom: -7px;
        font-size: 12px;
        // color: #ff4646; // 更柔和的红色
        white-space: nowrap; // 防止文字换行
      }

      // 输入框获得焦点时的样式
      &.van-field--error {
        .van-field__control {
          color: #ff4646;
        }
        
        .van-field__left-icon .van-icon {
          color: #ff4646;
        }
      }

      &:last-child {
        margin-bottom: 0;
        border-bottom: none;
      }

      .van-field__control {
        font-size: 15px; // 改为与登录页一致
        color: #333;
        height: 44px; // 改为与登录页一致

        &::placeholder {
          color: #999;
        }
      }

      .van-field__left-icon {
        display: flex;
        align-items: center;

        .van-icon {
          font-size: 18px;
          color: #666;
        }
      }
    }
  }

  .login-btn {
    width: 100%;
    height: 48px;
    font-size: 15px; // 统一字号为15px
    font-weight: 500;
    background: #8A2BE2;
    border: none;
    margin: 30px 0 24px;
  }

  .action-links {
    display: flex;
    justify-content: space-between;
    padding: 0 4px;

    .link {
      color: #8A2BE2;
      font-size: 14px;
      cursor: pointer;
      opacity: 0.9;

      &:active {
        opacity: 0.7;
      }
    }
  }
}

.other-login {
  .divider {
    text-align: center;
    color: #666;
    font-size: 14px;
    margin-bottom: 16px;
    position: relative;

    &::before,
    &::after {
      content: '';
      position: absolute;
      top: 50%;
      width: 25%;
      height: 1px;
      background: rgba(0, 0, 0, 0.1);
    }

    &::before {
      left: 16px;
    }

    &::after {
      right: 16px;
    }
  }

  .login-options {
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 0 4px;
  }

  .option-btn {
    width: 100%;
    height: 44px;
    background: rgba(255, 255, 255, 0.9);
    border: none;
    border-radius: 22px;
    color: #8A2BE2;
    font-size: 15px;
    cursor: pointer;
    transition: all 0.3s;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);

    &:active {
      background: rgba(255, 255, 255, 0.7);
      transform: scale(0.98);
    }
  }
}

</style>
























